/*
 * @Description:
 * @Author: 猪脚饭
 * @Date: 2022-11-16 08:28:45
 * @LastEditors: 猪脚饭
 * @LastEditTime: 2022-11-16 17:10:29
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'
import store from '@/store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    code: 'home',
    children: [{
      path: 'home',
      name: 'home',
      component: () => import('@/views/home/index')
    }]
  },
  {
    path: '/page',
    component: Layout,
    code: 'page',
    children: [{
      path: '',
      name: 'page',
      component: () => import('@/views/page/index')
    }]
  },
  {
    path: '/property/:title_contains/:category_contains/:agent_contains',
    component: Layout,
    code: 'property',
    children: [{
      path: '',
      name: 'property',
      component: () => import('@/views/property/index'),
      props: true,
      meta: { isRecord: true, top: 0 }
    }]
  },
  {
    path: '/detail/:id',
    component: Layout,
    code: 'detail',
    children: [{
      path: '',
      name: 'detail',
      component: () => import('@/views/detail/index'),
      props: true,
      meta: { isRecord: true, top: 0 }
    }]
  },
  {
    path: '/category',
    component: Layout,
    code: 'category',
    children: [{
      path: '',
      name: 'category',
      component: () => import('@/views/category/index')
    }]
  },
  {
    path: '/agents',
    component: Layout,
    code: 'agents',
    children: [{
      path: '',
      name: 'agents',
      component: () => import('@/views/agents/index'),
      meta: { isRecord: true, top: 0 }
    }]
  }
]

const router = new VueRouter({
  routes
})
// 导航守卫
router.beforeEach((to, from, next) => {
  // 保存当前路由
  store.commit('setRoutes', to.path)
  window.scrollY = 0
  next()
})
// 全局后置钩子
router.afterEach((to, from) => {
  // 如果当前的路由的元信息中，isRecord 的值为 true
  if (to.meta.isRecord) {
    setTimeout(() => {
      // 则把元信息中的 top 值设为滚动条纵向滚动的位置
      window.scrollTo(0, to.meta.top)
    }, 0)
  }
})
export default router
